import Vue from 'vue';
import VueRouter from 'vue-router'


Vue.use(VueRouter);
// 路由懒加载 需要的时候再加载，优化性能
let User = () => import('../page/User');
let ShopList = () => import('../page/ShopList');
let Seting = () => import('../page/Seting')
let Permiss = () => import('../page/Permiss')
let Goods = () => import('../page/Goods')
let NotFound = () => import('../page/NotFound')
let Son1 = () => import('../page/Son1')
let Son2 = () => import('../page/Son2')

let routes = [
    {
        path: '/',
        redirect:'/user'
    },
    {
        path: '/user',
        name: 'user',
        components: {
            aside: Seting,
            default:User
        }
    },
    {
        path: '/shoplist',
        name: 'shoplist',
        component: ShopList
    },
    {
        path: '/seting',
        name: 'seting',
        component: Seting
    },
    {
        path: '/permiss',
        name: 'permiss',
        component: Permiss
    },
    {
        path: '/goods',
        name: 'goods',
        component: Goods,
        children:[
            {
                path: '/goods/son1',
                component: Son1
            },
            {
                path: '/goods/son2',
                component: Son2
            }
        ],
        beforeEnter: (to, from, next) => {
            console.log('路由独享守卫');
            next()
        }
    },
    {
        path: '*',
        component:NotFound
    }

]


let router = new VueRouter({
    routes,
    mode: 'history',
    linkActiveClass:'active'
    
})

router.beforeEach((to, from, next) => {
    console.log(to);
    let arr = ['/shoplist', '/seting'];
    if (arr.indexOf(to.path) != -1) {
        alert('对不起你没有权限访问，请先登录');
        return next('/user')
    }
    console.log('全局前置守卫');
    next();
    document.title = to.name;
})
router.beforeResolve((to, from, next) => {
    console.log('全局解析守卫');
    next();
})

router.afterEach((to, from) => {
    console.log(to)
    console.log(from)
    console.log('全局后置钩子');
})


export default router;